﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.IO;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Pasi01")]
    public class Pasi01Controller : ApiController
    {   

        [HttpPost]
        [Route("PostPasi01_inq")]

        //多记录查询
        public System.Data.DataSet PostPasi01_inq([FromBody]  JObject obj)
        {

          

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();

             
//COMPANY_CODE
//PO_BILL_NO       
//PA_RECEIPT_NO
//BILL_STATUS
//STOCK_NO    
//REC_CREATOR
//SUPPLIER_CODE
//SUPPLIER_NAME

            inBlock.Tables[0].Columns.Add("S_FLAG");

            inBlock.Tables[0].Columns.Add("COMPANY_CODE");
            inBlock.Tables[0].Columns.Add("PO_BILL_NO");
            inBlock.Tables[0].Columns.Add("PA_RECEIPT_NO");
            inBlock.Tables[0].Columns.Add("BILL_STATUS");
            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("SUPPLIER_CODE");
            inBlock.Tables[0].Columns.Add("SUPPLIER_NAME");



            inBlock.Tables[0].Columns.Add("REC_CREATOR");
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");





            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            inBlock.sys_info.username = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            inBlock.sys_info.formname = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();

            inBlock.Tables[0].Rows[0]["S_FLAG"]             = "PASI0101";
            inBlock.Tables[0].Rows[0]["COMPANY_CODE"]       = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["PO_BILL_NO"]         = obj["PO_BILL_NO"] == null ? " " : obj["PO_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["PA_RECEIPT_NO"]      = obj["PA_RECEIPT_NO"] == null ? " " : obj["PA_RECEIPT_NO"].ToString();
            inBlock.Tables[0].Rows[0]["BILL_STATUS"]        = obj["BILL_STATUS"] == null ? " " : obj["BILL_STATUS"].ToString();
            inBlock.Tables[0].Rows[0]["STOCK_NO"]           = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString();
            inBlock.Tables[0].Rows[0]["SUPPLIER_CODE"]      = obj["SUPPLIER_CODE"] == null ? " " : obj["SUPPLIER_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["SUPPLIER_NAME"]      = obj["SUPPLIER_NAME"] == null ? " " : obj["SUPPLIER_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATOR"]        = obj["REC_CREATOR"] == null ? " " : obj["REC_CREATOR"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"]    = obj["REC_CREATE_TIME"] == null ? " " : obj["REC_CREATE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"]    = obj["REC_REVISE_TIME"] == null ? " " : obj["REC_REVISE_TIME"].ToString();   

           

            //仅查询最近最近2天的；不然数据太多，影响小程序效率；
            if (inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = DateTime.Now.AddMonths(-1).ToString("yyyyMMddHHmmss");
            }

            if (inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss");
            }





            EI.EIInfo outBlock = EI.EITuxedo.CallService("pasi93_inq", inBlock);

            //小代码转换；
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PA08");
            DataTable dtTemp = null;

            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t");

           //EI.EIInfo outBlock_TEMP3 = EF.Utility.ExecQuery("select companycode CODE ,companyname CODE_DESC_1_CONTENT from es.tescompanyinfo ");
           
        

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {  
                string STOCK_NO = outBlock.Tables[0].Rows[i]["STOCK_NO"].ToString();
                if (STOCK_NO.Trim() != "")
                {
                    dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + STOCK_NO + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["STOCK_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                //string COMPANY_CODE = outBlock.Tables[0].Rows[i]["COMPANY_CODE"].ToString();
                //if (COMPANY_CODE.Trim() != "")
                //{
                //    dtTemp = outBlock_TEMP3.Tables[0].Select("CODE  = '" + COMPANY_CODE + "'").CopyToDataTable<DataRow>();
                //    outBlock.Tables[0].Rows[i]["COMPANY_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                //} 


                string BILL_STATUS = outBlock.Tables[0].Rows[i]["BILL_STATUS"].ToString();
                if (BILL_STATUS.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["PA08"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }


                string PA_RECEIPT_DATE = outBlock.Tables[0].Rows[i]["PA_RECEIPT_DATE"].ToString();
                if (PA_RECEIPT_DATE.Length >= 8)
                    outBlock.Tables[0].Rows[i]["PA_RECEIPT_DATE"] = PA_RECEIPT_DATE.Substring(0, 4) + "-" + PA_RECEIPT_DATE.Substring(4, 2) + "-" + PA_RECEIPT_DATE.Substring(6, 2);



                string REC_CREATE_TIME = outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"].ToString();
                if (REC_CREATE_TIME.Length >= 8)
                    outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"] = REC_CREATE_TIME.Substring(0, 4) + "-" + REC_CREATE_TIME.Substring(4, 2) + "-" + REC_CREATE_TIME.Substring(6, 2);
                
            }

            
            
            outBlock.Tables[0].TableName = "Table0";
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        [HttpPost]
        [Route("PostPasi01_Info")]

        //单记录查询
        public System.Data.DataSet PostPasi01_Info([FromBody]  JObject obj)
        {

             EI.EIInfo inBlock = new EI.EIInfo();
           

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
             string PA_RECEIPT_NO = obj["PA_RECEIPT_NO"] == null ? " " : obj["PA_RECEIPT_NO"].ToString();
             string COMPANY_CODE = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
             string CONTRACT_KIND_PA = obj["CONTRACT_KIND_PA"] == null ? " " : obj["CONTRACT_KIND_PA"].ToString();




           




            //1* 采购申请单主项 + 子项查询*********************************************************


            inBlock.Tables[0].Columns.Add("PA_RECEIPT_NO");
             //inBlock.Tables[0].Columns.Add("COMPANY_CODE");
             //inBlock.Tables[0].Columns.Add("CONTRACT_KIND_PA");

            inBlock.Tables[0].Rows.Add();

            inBlock.Tables[0].Rows[0]["PA_RECEIPT_NO"] = PA_RECEIPT_NO;
            //inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = COMPANY_CODE;
            //inBlock.Tables[0].Rows[0]["CONTRACT_KIND_PA"] = CONTRACT_KIND_PA;


            inBlock.sys_info.company_code = "LT";

            



            inBlock.sys_info.userid     =  obj["USERID"] == null ? " ": obj["USERID"].ToString();
            inBlock.sys_info.username   = obj["USERNAME"].ToString();
            inBlock.sys_info.formname   = obj["FORMNAME"].ToString();

            EI.EIInfo outBlock = EI.EITuxedo.CallService("pasi0101_inq", inBlock);
             
            if (PA_RECEIPT_NO.Length < 10 || PA_RECEIPT_NO.Substring(0, 2) != "SR"
                || outBlock.Tables.Count <2 )
            { 

                outBlock.Tables.Add("T0");
                outBlock.Tables.Add("T1");



                outBlock.Tables.Add("SYS");
                outBlock.Tables["SYS"].Columns.Add("FLAG");
                outBlock.Tables["SYS"].Columns.Add("MSG");
                outBlock.Tables["SYS"].Rows.Add(-1, "无效的收货单["+ PA_RECEIPT_NO + "]");
                return outBlock;
            }
             


            










            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PA08","PA09", "FIBR", "FIDP");

           
           

            DataTable dtTemp = null;
            
            //主项代码转换；
            for (int i = outBlock.Tables[1].Rows.Count - 1; i >= 0; i--)
            {
                 
                string BILL_NO = outBlock.Tables[1].Rows[i]["PA_RECEIPT_NO"].ToString();
                string NAME_SUB = outBlock.Tables[1].Rows[i]["ASMAT_NO"].ToString();



                UpDownFileFtpController ctr = new UpDownFileFtpController();

                int files_num = ctr.PostGetFilesNum("PASI", BILL_NO, NAME_SUB);
                outBlock.Tables[1].Rows[i]["NO_STOCK_QTY"] = files_num;


                //var mapPath   = "D:\\pic\\_pic_new\\pasi\\" + BILL_NO.Substring(2, 3) + "\\" + BILL_NO + "\\";

                ////借用字段 NO_STOCK_QTY	未入库量(参考)，存放图片数量
                //outBlock.Tables[1].Rows[i]["NO_STOCK_QTY"] = 0;
                //if (Directory.Exists(mapPath))
                //{
                //    string[] file = Directory.GetFiles(mapPath, NAME_SUB + "*");
                //    outBlock.Tables[1].Rows[i]["NO_STOCK_QTY"] =  file.Length;
                //}


                 
               

                /*
                string branch_code = outBlock.Tables[1].Rows[i]["BRANCH_CODE"].ToString();
                if (branch_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                

                string dptment_code = outBlock.Tables[1].Rows[i]["DPTMENT_CODE"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                

                string BILL_STATUS = outBlock.Tables[1].Rows[i]["BILL_STATUS"].ToString();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA09"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                */


       
            }





            outBlock.Tables[0].TableName = "T0";
            outBlock.Tables[1].TableName = "T1";


            ////[增加主项]  T0 就是 TMAIN 
            //System.Data.DataSet  outBlock_main = PostPasi01_inq(obj);
            //outBlock_main.Tables[0].TableName = "TMAIN";
            //outBlock.Tables.Add(outBlock_main.Tables[0].Copy());
             
            //仓库
            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t where STOCK_NO='" + outBlock.Tables[0].Rows[0]["STOCK_NO"].ToString() + "'");
 
            if (outBlock_TEMP2.Tables[0].Rows.Count>0)
            {
                outBlock.Tables[0].Rows[0]["STOCK_NO"] = outBlock_TEMP2.Tables[0].Rows[0]["CODE_DESC_1_CONTENT"];
            }


            dtTemp = outBlock_TEMP.Tables["PA08"].Select("CODE  = '" + outBlock.Tables[0].Rows[0]["BILL_STATUS"].ToString() + "'").CopyToDataTable<DataRow>();
            outBlock.Tables[0].Rows[0]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


            string PA_RECEIPT_DATE = outBlock.Tables[0].Rows[0]["PA_RECEIPT_DATE"].ToString();
            if (PA_RECEIPT_DATE.Length >= 8)
                outBlock.Tables[0].Rows[0]["PA_RECEIPT_DATE"] = PA_RECEIPT_DATE.Substring(0, 4) + "-" + PA_RECEIPT_DATE.Substring(4, 2) + "-" + PA_RECEIPT_DATE.Substring(6, 2);



            string REC_CREATE_TIME = outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString();
            if (REC_CREATE_TIME.Length >= 8)
                outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = REC_CREATE_TIME.Substring(0, 4) + "-" + REC_CREATE_TIME.Substring(4, 2) + "-" + REC_CREATE_TIME.Substring(6, 2);

            
           

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }



     


        [HttpPost]
        [Route("PostPasi01_Load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostPasi01_Load([FromBody]  JObject obj)
        {           

            //小代码转换；
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("PA08");

            DataRow dr1 = outBlock.Tables["PA08"].NewRow();
            dr1["CODE"] = " ";
            dr1["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["PA08"].Rows.InsertAt(dr1, 0);

            EI.EIInfo inBlock = new EI.EIInfo();
            EI.EIInfo outBlock1 = EI.EITuxedo.CallService("pabm05_inq2", inBlock);
                      

            outBlock1.Tables[0].Columns[0].ColumnName = "CODE";
            outBlock1.Tables[0].Columns[1].ColumnName = "CODE_DESC_1_CONTENT";
           
            DataRow dr2 = outBlock1.Tables[0].NewRow();
            dr2["CODE"] = " ";
            dr2["CODE_DESC_1_CONTENT"] = " ";
            outBlock1.Tables[0].Rows.InsertAt(dr2, 0);
            outBlock1.Tables[0].TableName = "COMP";


            inBlock.sys_info.userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();          
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService("pasi01_inqck2", inBlock);

            outBlock2.Tables[0].Columns[0].ColumnName = "CODE";

            DataRow dr3 = outBlock2.Tables[0].NewRow();
            dr3["CODE"] = " ";
            dr3["CODE_DESC_1_CONTENT"] = " ";
            outBlock2.Tables[0].Rows.InsertAt(dr3, 0);
            outBlock2.Tables[0].TableName = "STOCK_NO";


            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());




            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        //[HttpPost]
        //[Route("PostPasi01_pro")]
 
    }
}